#!/bin/sh
#
# DracoPKG
#
# Copyright 2008 Ole Andre Rodlie <olear@dracolinux.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function dP_sysbuild () {
dP_input ${1}
INPUT=${1}
echo "Feature removed, please see dracosrc"
exit 1  
if [ -a ${DRACOSRC_DIR}/${INPUT}/Build ]; then
  echo
  echo "Start building package ${INPUT} ..."
  echo
else
  echo
  echo "Package not found."
  echo
  exit 1
fi

. ${DRACOSRC_DIR}/${INPUT}/Build || exit 1

if [ "${DRACOSRC_PKG_BLACKLIST}" ]; then
  echo
  echo "Package blacklisted."
  echo
  exit 1
fi

DRACOSRC_PKG_SRC_DIR=${DRACOSRC_DIR}/${INPUT}
DRACOSRC_PKG_WORK=${DRACOSRC_WORK}/${INPUT}
DRACOSRC_PKG=${DRACOSRC_PKG_WORK}/package

if [ -d ${DRACOSRC_PKG_WORK} ]; then
  echo -n "=> Removing work directory ... "
  rm -rf ${DRACOSRC_PKG_WORK}
  echo "OK"
else
  mkdir -p ${DRACOSRC_PKG_WORK}
fi  

if [ ! -d ${DRACOSRC_PKG} ]; then
  mkdir -p ${DRACOSRC_PKG}
fi

if [ -a ${DRACOSRC_PKG_SRC_DIR}/framework.tar.gz ]; then
  echo "Found framework!"
  ( cd $DRACOSRC_PKG ;
    ${PKGTOOLS_EXPLODE} ${DRACOSRC_PKG_SRC_DIR}/framework.tar.gz
  )
fi

if [ ! "${DRACOSRC_NOSOURCE}" ]; then

  if [ "${DRACOSRC_PKG_ALT_NAME}" ]; then
    DRACOSRC_PKG_DWN=${DRACOSRC_PKG_ALT_NAME}
  else
    DRACOSRC_PKG_DWN="${DRACOSRC_PKG_NAME}-"
  fi

  if [ "${DRACOSRC_PKG_ALT_VERSION}" ]; then
    DRACOSRC_PKG_DWN_VERSION=${DRACOSRC_PKG_ALT_VERSION}
  else
    DRACOSRC_PKG_DWN_VERSION=${DRACOSRC_PKG_VERSION}
  fi

  dP_cache ${DRACOSRC_CACHE}/${DRACOSRC_PKG_DWN}${DRACOSRC_PKG_DWN_VERSION}.${DRACOSRC_PKG_SOURCE_EXT}

  if [ ${DRACOSRC_PKG_SOURCE_EXT} = "tar.gz" ]; then
    TAR="tar xzf"
  fi
  if [ ${DRACOSRC_PKG_SOURCE_EXT} = "tgz" ]; then
    TAR="tar xzf"
  fi
  if [ ${DRACOSRC_PKG_SOURCE_EXT} = "tar.bz2" ]; then
    TAR="tar xjf"
  fi
  
  echo -n "=> Extracting source ... "
  ${TAR} ${DRACOSRC_CACHE}/${DRACOSRC_PKG_DWN}${DRACOSRC_PKG_DWN_VERSION}.${DRACOSRC_PKG_SOURCE_EXT} -C ${DRACOSRC_PKG_WORK} || exit 1
  echo "OK"

  if [ "${DRACOSRC_PKG_ALT_PKG_SRC}" ]; then
    DRACOSRC_PKG_SRC=${DRACOSRC_PKG_WORK}/${DRACOSRC_PKG_ALT_PKG_SRC}
  else
    DRACOSRC_PKG_SRC=${DRACOSRC_PKG_WORK}/${DRACOSRC_PKG_DWN}${DRACOSRC_PKG_DWN_VERSION}
  fi

  cd ${DRACOSRC_PKG_SRC} || exit 1

  if [ -f ${DRACOSRC_DIR}/${INPUT}/Patch ]; then
    echo "=> Executing Patch file ..."
    . ${DRACOSRC_DIR}/${INPUT}/Patch
  fi

  if [ -a $DRACOSRC_PKG_SRC_DIR/diff ]; then
    if [ -a $DRACOSRC_PKG_SRC_DIR/diff/2 ]; then
      for i in `ls $DRACOSRC_PKG_SRC_DIR/diff/2`; do
        patch -p2 -l < $DRACOSRC_PKG_SRC_DIR/diff/2/$i || exit 1
      done   
    fi
    if [ -a $DRACOSRC_PKG_SRC_DIR/diff/1 ]; then
      for i in `ls $DRACOSRC_PKG_SRC_DIR/diff/1`; do
        patch -p1 -l < $DRACOSRC_PKG_SRC_DIR/diff/1/$i || exit 1
      done
    fi
    if [ -a $DRACOSRC_PKG_SRC_DIR/diff/0 ]; then
      for i in `ls $DRACOSRC_PKG_SRC_DIR/diff/0`; do
        patch -p0 -l < $DRACOSRC_PKG_SRC_DIR/diff/0/$i || exit 1
      done
    fi
  fi

  if [ -f ${DRACOSRC_DIR}/${INPUT}/PreConf ]; then
    echo "=> Executing PreConf file ..."
    . ${DRACOSRC_DIR}/${INPUT}/PreConf
  fi

  chown -R root:root .
  find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

  if [ -f ${DRACOSRC_DIR}/${INPUT}/Custom ]; then
    echo "=> Executing Custom file ..."
    . ${DRACOSRC_DIR}/${INPUT}/Custom
  else
    if [ "${DRACOSRC_PKG_USE_AUTOCONF}" ]; then
      autoconf
    fi
    CFLAGS="${DRACOSRC_FLAGS}" CXXFLAGS="${DRACOSRC_FLAGS}" ./configure ${DRACOSRC_PKG_CONFIGURE} || exit 1
    if [ -f ${DRACOSRC_DIR}/${INPUT}/Make ]; then
      echo "=> Executing Make file ..."
      . ${DRACOSRC_DIR}/${INPUT}/Make
    else
      make -j3 || exit 1
      make install DESTDIR=${DRACOSRC_PKG} || exit 1
    fi
  fi

fi

if [ -f ${DRACOSRC_DIR}/${INPUT}/Extra ]; then
  echo "=> Executing Extra file ..."
  . ${DRACOSRC_DIR}/${INPUT}/Extra
fi

( cd ${DRACOSRC_PKG} ;
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null 
)

if [ -a $DRACOSRC_PKG/usr/info ]; then
( cd $DRACOSRC_PKG/usr/info ;
  if [ -a dir ]; then
  if [ ! "$DRACOSRC_PKG_NAME" = "texinfo" ]; then
  rm dir
  fi
  fi
  gzip -9 *
  if [ -a dir.gz ]; then
    gunzip dir.gz
  fi
)
fi   

if [ ! "$DRACOSRC_NOMANGZ" ]; then
  if [ -a $DRACOSRC_PKG/usr/man ]; then
    ( cd $DRACOSRC_PKG/usr/man ;
      for manpagedir in $(find . -type d -name "man*") ; do
        ( cd $manpagedir ;
          for eachpage in $( find . -type l -maxdepth 1) ; do
            ln -s $( readlink $eachpage ).gz $eachpage.gz
            rm $eachpage
          done
          gzip -9 *.*
        )
      done
    )
  fi
fi

# Breaks mkinitrd
#if [ -a $DRACOSRC_PKG/lib/modules ]; then
#  find $DRACOSRC_PKG/lib/modules -type f -name "*.ko" -exec gzip -9f {} \;
#fi

( cd $DRACOSRC_PKG;
  for svn in $(find . -type d -name ".svn" -maxdepth 50) ; do
    rm -rf $svn
  done
  find . -type f -name "*~" -exec rm {} \;
)

mkdir -p $DRACOSRC_PKG/usr/doc/$DRACOSRC_PKG_NAME-$DRACOSRC_PKG_VERSION
( cd ${DRACOSRC_PKG_SRC} ; cp -a RELNOTES* COPYING* AUTHORS* README* ChangeLog* LICENSE* $DRACOSRC_PKG/usr/doc/$DRACOSRC_PKG_NAME-$DRACOSRC_PKG_VERSION/ )

mkdir -p $DRACOSRC_PKG/install
cat >$DRACOSRC_PKG/install/slack-desc <<EOF   
$DRACOSRC_PKG_NAME: ${DRACOSRC_PKG_NAME}
$DRACOSRC_PKG_NAME:
$DRACOSRC_PKG_NAME: ${DRACOSRC_PKG_DESC}
$DRACOSRC_PKG_NAME:
$DRACOSRC_PKG_NAME:
$DRACOSRC_PKG_NAME:
$DRACOSRC_PKG_NAME:
$DRACOSRC_PKG_NAME:
$DRACOSRC_PKG_NAME: 
$DRACOSRC_PKG_NAME: 
$DRACOSRC_PKG_NAME:
EOF

if [ -a ${DRACOSRC_DIR}/${INPUT}/Install ]; then
  cat ${DRACOSRC_DIR}/${INPUT}/Install > $DRACOSRC_PKG/install/doinst.sh
fi
                  
cd $DRACOSRC_PKG
DRACOSRC_PKG_TGZ=${DRACOSRC_PACKAGES}/${DRACOSRC_PKG_NAME}-${DRACOSRC_PKG_VERSION}-${DRACOSRC_ARCH}-${DRACOSRC_TAG}${DRACOSRC_PKG_BUILD}.tgz
${PKGTOOLS_MAKE} -l y -c n ${DRACOSRC_PKG_TGZ}

if [ -a ${DRACOSRC_PKG_TGZ} ]; then
  read -p "Install package (yes/no)? "
  if [ "${REPLY}" = "yes" ]; then
    ${PKGTOOLS_UPGRADE} --install-new ${DRACOSRC_PKG_TGZ}
  fi
fi
   
}

